package cn.jinjimi.app.custom.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.custom.entity.Custom;
import cn.jinjimi.app.custom.manager.CustomsManager;
import cn.jinjimi.app.main.dialog.CommAlertDialog;
import cn.jinjimi.app.main.dialog.LoadingDialog;

/**
 * Created by Roy
 * Date: 16/6/3
 */
public class CustomArchivesMenuWindow extends PopupWindow implements View.OnClickListener {

    private Context mContext;
    private Custom mCustom;

    public CustomArchivesMenuWindow(Context context, Custom custom) {
        this.mContext = context;
        this.mCustom = custom;

        View view = View.inflate(context, R.layout.view_customer_archives_menu_layout, null);
        setContentView(view);

        initListener(view);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setFocusable(true);
        setTouchable(true);
    }

    private void initListener(View view) {
        view.findViewById(R.id.more_menu_delete).setOnClickListener(this);

        view.findViewById(R.id.more_menu_bg).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more_menu_delete://删除客户
                showCustomDelDialog();
                break;
        }
        dismiss();
    }


    /* 弹出删除框 */
    private void showCustomDelDialog() {
        final CommAlertDialog commAlertDialog = new CommAlertDialog(mContext, R.style.TransparentDialog);
        commAlertDialog.setTitle(mContext.getString(R.string.affirm_delete))
                .setRightBtn(mContext.getString(R.string.ok), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        deleteCustom();

                        commAlertDialog.dismissDialog();
                    }
                });
        commAlertDialog.showDialog(CommAlertDialog.Style.TWO_BUTTON);
    }

    /* 删除客户 */
    @EventTrace(value = EventID.APP_CUSTOMER_DETAIL_MENU_DELUSER, paramsK = {EventDataField.ParamClick}, paramsV = {EventDataField.ClickDelusr})
    private void deleteCustom() {
        LoadingDialog.show(mContext, mContext.getString(R.string.deleting_wait));//提示正在删除
        CustomsManager.getInstance().deleteCustom(mCustom, false);
    }
}
